/*
 * Copyright (c) 2025 彭友聪
 * nativePC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 *
 * Author: 彭友聪
 * email：2923616405@qq.com
 * date: 2025/10/8 09:27
 * file: ${FILE_NAME}
 * product: DevEco Studio
 * LICENSE: MulanPSL-2.0
 * */
//
// Created on 2025/10/8.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#include "TestDragImageExample.h"
#include "TestGestureExample.h"
#include "napi/native_api.h"
#include <arkui/native_node_napi.h>
#include <js_native_api.h>
#include "NativeEntry.h"
#include "NormalTextListExample.h"
#include "testAnimate.h"
extern napi_env g_env;
namespace NativeModule
{
    napi_value CreateNativeRoot(napi_env env, napi_callback_info info)
    {
        size_t argc = 1;
        napi_value args[1] = {nullptr};

        napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

        // 获取 NodeContent
        ArkUI_NodeContentHandle contentHandle;
        OH_ArkUI_GetNodeContentFromNapiValue(env, args[0], &contentHandle);
        NativeEntry::GetInstance()->SetContentHandle(contentHandle);

        // 创建文本列表
//        auto list = CreateTextListExample();
//        NativeEntry::GetInstance()->SetRootNode(list);
        // 创建 Column
//    auto column = testGestureExample();
//    NativeEntry::GetInstance()->SetRootNode(column);
        // 创建 image
//        auto image = CreateDragImageExample();
//        NativeEntry::GetInstance()->SetRootNode(image);
        // 演示动画
        auto column = testAnimate();
        NativeEntry::GetInstance()->SetRootNode(column);
        return nullptr;
    }

    napi_value DestroyNativeRoot(napi_env env, napi_callback_info info)
    {
        NativeEntry::GetInstance()->DisposeRootNode();
        napi_delete_reference(g_env, g_clickCallbackRef);
        return nullptr;
    }
    void showUITextCallback(std::string tip, std::string content)
    {
        globalParams->value = &content;
        globalParams->desc = tip;
        std::string data = globalParams->desc + ": " + *globalParams->value;
        napi_env env = g_env; // 获取当前napi环境
        napi_value callback;
        napi_get_reference_value(env, g_clickCallbackRef, &callback);

        // 构造传递参数
        napi_value argv;
        napi_create_string_utf8(env, data.c_str(), data.length(), &argv);

        // 调用ArkTS回调
        napi_value result;
        napi_call_function(env, nullptr, callback, 1, &argv, &result);
    }
    ArkUI_ContextHandle getGlobalUIContext()
    {
        return globalUIContext;
    }
}